/**
 * Created by Administrator on 2020/6/3.
 * Util
 */


let Util=function(){


};

/**获取网页参数**/
Util.prototype.GetUrlParam=function(a) {
    var b = new RegExp("(^|&)" + a + "=([^&]*)(&|$)","i")
        , c = window.location.search.substr(1).match(b);
    return null != c ? unescape(c[2]) : "";
}
/**获取cookie**/
Util.prototype.getCookie=function(name) {
    let arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
    if(arr=document.cookie.match(reg))
        return unescape(arr[2]);
    else
        return "";
}

/**
 return 端ID(0=PC,1=安卓,2=IOS)
 **/
Util.prototype.getAppType=function() {
    //区别端
    let web_Appid=1;//端ID(0=PC,1=安卓,2=IOS)
    if ((navigator.userAgent.match(/(iPhone|iPod|Android|ios|iOS|iPad|Backerry|WebOS|Symbian|Windows Phone|Phone)/i))) {
        console.log("手机访问.");
        web_Appid=1
    }else{
        console.log("电脑访问.");
        web_Appid=0;
    }
    /*if(web_Appid!=0){
        $('.closeBtn').css("display","block");
    }*/
    let web_userAgent= navigator.userAgent; //识别各种浏览器
    let web_isAndroid = web_userAgent.indexOf('Android') > -1 || web_userAgent.indexOf('Adr') > -1; //android终端
    let web_isiOS = !!web_userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
    if (web_isAndroid) {
        web_Appid=1;
    } else if (web_isiOS) {
        web_Appid=2;
    }
    return web_Appid;
}
Util.prototype.getDomainName=function() {
    var a = window.location.host.split(".");
    return a[a.length - 2]
}
/**
 * 截取字符串
 * len 截取长度
 */
Util.prototype.interceptCharacter=function(str,len){
    if(str==null)str="";
    //trace("CarUtil interceptCharacter",str);
    let i=0;
    let _end=0;
    let _len=0;
    for (i = 0; i < str.length; i++)
    {

        if(str.charCodeAt(i)>10000){
            _len+=2;
        }else{
            _len+=1;
        }
        if(_len<=len){
            _end++;
        }

    }
    if(_end<1)_end=1;
    let _str="";
    for (i = 0; i < _end; i++)
    {
        _str+=str.charAt(i);
    }
    if(_str.length<str.length)_str=_str+"...";
    return _str;
}
/**获取 css类class里的Rotate**/
Util.prototype.getCalssRotate=function(elId){
    let el = document.getElementById(elId);
    //console.log(turnId,el);
    let st = window.getComputedStyle(el, null);
    let tr = st.getPropertyValue("-webkit-transform") || st.getPropertyValue("-moz-transform") || st.getPropertyValue("-ms-transform") || st.getPropertyValue("-o-transform") || st.getPropertyValue("transform") || "FAIL";
    //console.log('Matrix: ' + tr);
    let values = tr.split('(')[1].split(')')[0].split(',');
    let a = values[0];
    let b = values[1];
    let c = values[2];
    let d = values[3];
    let scale = Math.sqrt(a * a + b * b);
    //console.log('Scale: ' + scale);
    let sin = b / scale;
    let angle = Math.round(Math.atan2(b, a) * (180 / Math.PI));
    //console.log('Rotate: ' + angle + 'deg');
    return angle;
}
/**加载本土txt,public文件夹下**/
Util.prototype.loadFile = function (name) {
    // name为文件所在位置
    let xhr = new XMLHttpRequest()
    const okStatus = document.location.protocol === 'file:' ? 0 : 200
    xhr.open('GET', name, false)
    xhr.overrideMimeType('text/html;charset=utf-8') //默认为utf-8
    xhr.send(null)
    return xhr.status === okStatus ? xhr.responseText : '';
}

/** 获取元素的绝对位置坐标（像对于页面左上角） 单位:px **/
Util.prototype.getElementPagePosition=function(element){
    //计算x坐标
    let actualLeft = element.offsetLeft;
    let current = element.offsetParent;
    while (current !== null){
        actualLeft += current.offsetLeft;
        current = current.offsetParent;
    }
    //计算y坐标
    let actualTop = element.offsetTop;
    current = element.offsetParent;
    while (current !== null){
        actualTop += (current.offsetTop+current.clientTop);
        current = current.offsetParent;
    }
    //返回结果
    return {x: actualLeft, y: actualTop}
}
/**返回时间字符串**/
Util.prototype.getTimeStr=function(time){
    let _date=new Date(time);
    let _mon=(_date.getMonth()+1);
    if(_mon<10)_mon='0'+_mon;
    let _ri=_date.getDate()+'';
    if(_date.getDate()<10)_ri='0'+_ri;
    let _hours=_date.getHours()+'';
    if(_date.getHours()<10)_hours='0'+_hours;
    let _miu=_date.getMinutes()+'';
    if(_date.getMinutes()<10)_miu="0"+_miu;
    let str=_date.getFullYear()+'-'+_mon+'-'+_ri+' '+_hours+":"+_miu;
    return str;
}
/**返回时间字符串**/
Util.prototype.getTimeStr_2=function(time){
    let _date=new Date(time);
    let _mon=(_date.getMonth()+1);
    if(_mon<10)_mon='0'+_mon;
    let _ri=_date.getDate()+'';
    if(_date.getDate()<10)_ri='0'+_ri;
    let _hours=_date.getHours()+'';
    if(_date.getHours()<10)_hours='0'+_hours;
    let _miu=_date.getMinutes()+'';
    if(_date.getMinutes()<10)_miu="0"+_miu;
    let str=_hours+":"+_miu;
    return str;
}



export default Util;

